//
//  HYYMainTabBar.swift
//  Hyy9Weibo
//
//  Created by Air on 16/3/24.
//  Copyright © 2016年 hyy. All rights reserved.
//

import UIKit

//tabBarItem个数
let tabBarItemCount = 5

class HYYMainTabBar: UITabBar {

    override func layoutSubviews() {
        super.layoutSubviews()
//        print("HYYMainTabBar : layoutSubviews")
            
        //计算item宽度
        let width = frame.width / CGFloat(tabBarItemCount)
//        print("subviews : \(subviews)")
        var index = 0
        for view in subviews {
            //判断是否是UITabBarButton
            let cls = NSClassFromString("UITabBarButton")
            if view.isKindOfClass(cls!) {
//                print("要设置的view : \(view)")
            //设置UITabBarButton的位置
                view.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: frame.height)
                index += index == 1 ? 2 : 1
            }
            
        }
        //设置加号按钮
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: frame.height)
    }
    
    
    //MARK: - 懒加载
    //加号按钮
    private lazy var composeButton: UIButton = {
        let button = UIButton()
        //设置
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //添加到tabbar
        self.addSubview(button)
        return button
    }()
}
